package com.apes.fn.scm.internal.help.service.voucher.sale;

import com.apes.fn.scm.internal.help.model.InternalHelpReturnApproval;
import com.apes.fn.scm.internal.help.model.InternalHelpReturnApprovalItem;
import com.apes.framework.api.DomainService;
import com.apes.framework.util.MapUtil;
import com.apes.scm.account.account.model.InventoryBatch;
import com.apes.scm.account.account.service.InventoryService;
import com.apes.scm.sal.saleOrder.SaleOrder;
import com.apes.scm.sal.saleOrder.SaleOrderItem;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherItem;
import com.apes.scm.voucher.service.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 分销单服务商品返厂
 *
 * @Author wuq
 * @Date 2021-10-19
 */
@Service
public class SaleAutReturnVoucherService extends DomainService {

    @Autowired
    private InventoryService inventoryService;

    public void process(Map<SaleOrderItem, InternalHelpReturnApprovalItem> param) {
        List<SaleOrderItem> saleOrderItems = new ArrayList<>(param.keySet());

        List<InternalHelpReturnApprovalItem> returnItems = new ArrayList<>(param.values());
        InternalHelpReturnApproval internalHelpReturnApproval = returnItems.get(0).getApproval();

        Voucher voucher = new Voucher();
        voucher.setReturned(true);
        voucher.setBusinessType(BusinessType.PURCHASE_RETURN);

        // 获取分销单数据
        SaleOrder saleOrder = saleOrderItems.get(0).getSaleOrder();

        voucher.setDept(saleOrder.getDept());
        voucher.setCompany(saleOrder.getCompany());
        voucher.setPartner(saleOrderItems.get(0).getSupplier());    // 由于分销单生成凭证时，会回写供应商，这里直接取

        // 通过批次获取部门
        InventoryBatch inventoryBatch = saleOrderItems.get(0).getInventoryBatch();
        voucher.setInventoryMode(inventoryBatch.getCooperation().getInventoryMode());
        voucher.setAccountingMethod(inventoryBatch.getCooperation().getAccountingMethod());
        voucher.setContract(inventoryBatch.getContract());

        voucher.setScene(internalHelpReturnApproval.getScene());
        voucher.setOrigin(internalHelpReturnApproval.getId());
        voucher.setOriginBusinessType(internalHelpReturnApproval.getInnerBusinessType());
        voucher.setSource(internalHelpReturnApproval.getId());
        voucher.setSourceBusinessType(internalHelpReturnApproval.getInnerBusinessType());
        voucher.setClerk(internalHelpReturnApproval.getApplyUid());
        voucher.setCustomerExpand(internalHelpReturnApproval.getCustomerExpand().getId());

        voucher.setVoucherItems(
                returnItems.stream().map(
                        item -> {
                            VoucherItem voucherItem = new VoucherItem();
                            voucherItem.setProduct(item.getProduct());

                            voucherItem.setUom(item.getUom());
                            voucherItem.setProductQty(-1.00 * item.getProductReturnQty());
                            voucherItem.setOriginItemId(String.valueOf(item.getId()));
                            voucherItem.setSourceItemId(String.valueOf(item.getId()));

                            SaleOrderItem saleOrderItem = saleOrderItems.stream().filter(saleItem -> Objects.equals(saleItem.getId(), item.getOriginItemId())).findFirst().get();
                            voucherItem.setInventoryBatch(saleOrderItem.getInventoryBatch());
                            voucherItem.setPriceUnit(saleOrderItem.getInventoryBatch().getTaxCostPrice());
                            voucherItem.setTax(saleOrderItem.getInventoryBatch().getTax());
                            voucherItem.setOwner(saleOrder.getCompany());

                            //  增加（减少）冻结数量
                            inventoryService.changeFrostQty(saleOrderItem.getInventoryBatch(), item.getUom(), item.getProductReturnQty(), inventoryBatch.getCompany(),
                                    internalHelpReturnApproval.getInnerBusinessType(), internalHelpReturnApproval.getId(), item.getId(), "门店经销带安返厂凭证");

                            return voucherItem;
                        }).collect(Collectors.toSet()));

        invoke("voucher.create", MapUtil.mapped(voucher));
    }

}
